<?php

// this script can take several minutes to execute
set_time_limit(600);

$dirname = str_replace(DIRECTORY_SEPARATOR, '/', dirname(__FILE__));

if(!class_exists('Antz')) die('Antz tookit is required: http://dev.cmsmadesimple.org/projects/antz/');
$DB = Antz::registry('db');
$USER = Antz::registry('user');
$CRYPT = Antz::registry('crypt');

// first, we uninstall previous version to start with a clean slate
$this->Uninstall();

// define the gateways that come packaged
$defaultGateways = array(
	'ISubscribe_PayPal_Website_Payments_Standard_1' => 'Paypal Website Payments Standard'
);

//$DB->debug(true);
//$DB->showErrors(true);
//$haltInstall = true;

global $gCms;


$error = false;



if($gCms->config['dbms']=='postgres7'){
    // postgresql setup

    $tablesFile = 'tables.pgsql.sql';
    ob_start();
    include($tablesFile);
    $tableDefinition = ob_get_contents();
    ob_end_clean();


    // add the system prefix to the tables
    $tableDefinition = str_replace('prefix_', cms_db_prefix(), $tableDefinition);

    $tableDefinition = explode(';', $tableDefinition);
    foreach($tableDefinition as $k=>$v){
        $DB->query($v);
    };

    // add the config settings

        ob_start();
        include($dirname.'/config.sql');
        $config = ob_get_contents();
        ob_end_clean();
        $config = str_replace('prefix_', cms_db_prefix(), $config);
        $config = explode(';', $config);
        foreach($config as $k=>$v){
            $DB->query($v);
        };


    // ending postgresql setup

}else if($gCms->config['dbms']=='mysql'){
    // mysql setup

    $tablesFile = 'tables.mysql.sql';
    ob_start();
    include($tablesFile);
    $tableDefinition = ob_get_contents();
    ob_end_clean();


    // add the system prefix to the tables
    $tableDefinition = str_replace('prefix_', cms_db_prefix(), $tableDefinition);
    $tableDefinition = explode(';', $tableDefinition);
    foreach($tableDefinition as $k=>$v){
        $DB->query($v);
    };

    // add the config settings

        ob_start();
        include($dirname.'/config.sql');
        $config = ob_get_contents();
        ob_end_clean();
        $config = str_replace('prefix_', cms_db_prefix(), $config);
        $config = explode(';', $config);
        foreach($config as $k=>$v){
            $DB->query($v);
        };

    // ending mysql setup

};


// create our permissions in the system that will enable us to restrict access to the module
$this->CreatePermission($this->GetName().' Config', $this->GetName().' Config');
$this->CreatePermission($this->GetName().' Manage', $this->GetName().' Manage');
$this->CreatePermission($this->GetName().' PHP', $this->GetName().' Edit PHP code');
$this->CreatePermission($this->GetName().' Users', $this->Lang('perm_manage_users'));

// now lets install the gateways
foreach($defaultGateways as $className => $title){
	$filename = $this->dirname.'/gateways/'.$className.'.php';
	require_once($filename);
	if(!class_exists($className)) continue;
	$gwObj = new $className;
	$dbParams = array(
		'title' => $title,
		'class_name' => $className,
		'obj' => $CRYPT->crypt(serialize($gwObj)),
		'description' => $title,
		'is_active' => '0'
	);
	$DB->insert(cms_db_prefix().'isubscribe_payment_gateways', $dbParams);
}













if($haltInstall) exit;

